UserPhoto = function (config) {
    if (typeof config == 'object') {
        for (var prop in config) {
            this[prop] = config[prop];
        }
    }
    this.uploadedPhotos = new Array();
    for (var i = 0; i < this.maxPhotoCount; ++i) {
        this.uploadedPhotos[i] = (i >= config.loaded ? 0 : 1);
    }
    if (config.loaded >= config.maxPhotoCount) {
        $('#download-photo').hide();
    }
    this.isEdit = config.isEdit;
};
UserPhoto.prototype.maxPhotoCount = 20;
UserPhoto.prototype.photosInRow = 4;
UserPhoto.prototype.removeUrl = '/ajax/remove_photo.php';
UserPhoto.prototype.countPhotos = function () {
    for (var i = 0; i < this.maxPhotoCount; ++i) {
        if (this.uploadedPhotos[i] == 0) {
            return i;
        }
    }
    return this.maxPhotoCount;
};
UserPhoto.prototype.getDeleteOnClickHandler = function (scope, i, unifiedid) {
    return function () {
        scope.remove(i, unifiedid);
        return false;
    };
};
UserPhoto.prototype.getDeleteOnClickHandlerForEdit = function (scope, i, unifiedid) {
    return function () {
        scope.remove(i, unifiedid, 'session only');
        return false;
    };
};
UserPhoto.prototype.getSetMainOnClickHandler = function (scope, i) {
    return function () {
        scope.setMainPhoto(i);
        return false;
    };
};
UserPhoto.prototype.setMainPhoto = function (i) {
    if (this.mainType == 'radio') {
        this.clearSelected();
    }
    var li = $('#downloaded-photo-' + i);
    if (li.is('.act')) {
        $('#downloaded-photo-' + i).removeClass('act');
        li.find('input').first().attr('checked', false);
    } else {
        $('#downloaded-photo-' + i).addClass('act');
        li.find('input').first().attr('checked', 'checked');
    };
};
UserPhoto.prototype.clearSelected = function () {
    $('#photos > li._withPhoto').removeClass('act');
};
UserPhoto.prototype.getNextNodeParent = function () {
    return $('#photos');
};
UserPhoto.prototype.showUploadBlock = function () {
    var i = this.countPhotos();
    if (($("#input-file-upload").val() == '') || (i == (this.maxPhotoCount))) {
        alert('Вы не можете загрузить более ' + this.maxPhotoCount + ' фото.');
        return false;
    }
    $('#upload-photo-form').submit();
    this.swapProgressLoadingBlocks();
};
UserPhoto.prototype.swapProgressLoadingBlocks = function () {
    if (!$('#wait-block').is(':visible')) {
        $('#wait-block').show();
        $('#upload-photo-form').hide();
    } else {
        $('#wait-block').hide();
        $('#upload-photo-form').show();
    }
};
UserPhoto.prototype.hideProgressLoadingBlocks = function () {
    $('#wait-block').hide();
    $('#upload-photo-form').show();
};
UserPhoto.prototype.hideUploadBlock = function () {
    $('#input-file-upload').val('');
    this.swapProgressLoadingBlocks();
    var i = this.countPhotos();
    if (i == this.maxPhotoCount) {
        return false;
    }
    var block = $('#downloaded-photo-' + Number(i + 1));
    if (!block) return false;
    block.parent().find(block).remove();
};
UserPhoto.prototype.doneUpload = function (src, width, height, tempname, unifiedid, caption) {
    $('#input-file-upload').val('');
    var i = this.countPhotos();
    if (i == this.maxPhotoCount) {
        return false;
    }
    var node = $('#downloaded-photo-').clone(true);
    var img = node.find('img').first();
    var link = node.find('a.ico-close');
    var filenameRadio = node.find('input').first();
    var filename = node.find('input[type=hidden]');
    node.attr('id', node.attr('id') + (i + 1));
    img.attr('src', src);
    img.attr('width', width);
    img.attr('height', height);
    filename.attr('id', filename.attr('id') + (i + 1));
    filename.val(tempname);
    filenameRadio.val(tempname);
    node.find('a._setMain').click(this.getSetMainOnClickHandler(this, i + 1));
    filenameRadio.change(this.getSetMainOnClickHandler(this, i + 1));
    link.click(this.getDeleteOnClickHandler(this, i, unifiedid));
    if (unifiedid) {
        link.attr('unifiedid', unifiedid);
    }
    this.getNextNodeParent().children('li').last().before(node);
    node.show();
    if (this.countPhotos() == (this.maxPhotoCount - 1)) {
        $('#download-photo').hide();
    } else {
        $('#upload-photo-form').show();
        $('#wait-block').hide();
    }
    this.uploadedPhotos[i] = 1;
    this.resetMainPhoto();
};
UserPhoto.prototype.remove = function (i, unifiedid) {
    var block = $('#downloaded-photo-' + (i + 1));
    var src = block.find('img').first().attr('src');
    var parentNode = block.parent();
    parentNode.find(block).remove();
    var count = this.countPhotos();
    for (var j = i + 1; j < count; ++j) {
        var node = $('#downloaded-photo-' + (j + 1));
        node.attr('id', 'downloaded-photo-' + j);
        var link = node.find('a.ico-close');
        var linkUnifiedId = link.attr('unifiedid');
        link.unbind('click');
        if (this.isEdit) {
            link.click(this.getDeleteOnClickHandlerForEdit(this, j - 1, linkUnifiedId));
        } else {
            link.click(this.getDeleteOnClickHandler(this, j - 1, linkUnifiedId));
        }
        node.find('a._setMain').unbind('click').click(this.getSetMainOnClickHandler(this, j));
        node.find('input:visible').unbind('change').change(this.getSetMainOnClickHandler(this, j));
        var filename = node.find('input[type=hidden]');
        filename.attr('id', 'filename-' + j);
    }
    this.uploadedPhotos[count - 1] = 0;
    if (this.countPhotos() <= (this.maxPhotoCount - 1)) {
        $('#download-photo').show();
    }
    this.resetMainPhoto();
    $.post(this.removeUrl, {
        photo: src,
        id: i,
        unid: unifiedid,
        rand: (this.rand ? this.rand : 0),
        op: (arguments[2] ? arguments[2] : '')
    });
    return false;
};
UserPhoto.prototype.resetMainPhoto = function () {
    if (this.mainType == 'checkbox') return;
    var checked = false;
    var photoInputs = $('#photos').find('input:radio');
    for (var i = 0; i < photoInputs.length; i++) {
        if ($(photoInputs[i]).is('input[type=radio]:visible')) {
            if ($(photoInputs[i]).attr('name', 'main_photo')) {
                if ($(photoInputs[i]).is(':checked')) {
                    checked = true;
                }
            }
        }
    }
    if (!checked) {
        for (var i = 0;
        (i < photoInputs.length) && (!checked); i++) {
            if ($(photoInputs[i]).is('input[type=radio]:visible')) {
                if ($(photoInputs[i]).attr('name', 'main_photo')) {
                    checked = true;
                    $(photoInputs[i]).attr('checked', true);
                    $(photoInputs[i]).parent().parent().addClass('act');
                }
            }
        }
    }
};